﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using hedefgrup.reports.renault.calculators;
using hedefgrup.reports.renault;
using System.Text;
using hedefgrup.reports.renault.definitions;
using System.Collections.Generic;
using hedefgrup.reports.renault.sorting;

public partial class jsCmd_dealerList : reportPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        base.initialize();

        bool displayVirtualDealersOnly = base.getString("v") == "1";

        Response.Write("clearDropDown('d');");

        if (displayVirtualDealersOnly && base.calculationType == calculatorType.connectedPoints)
        {
            Response.Write("hideControl('d');");
            return;
        }

        Response.Write("showControl('d');");

        string renderedContent = renderDealerList(displayVirtualDealersOnly);

        string disabled = "";

        if (string.IsNullOrEmpty(renderedContent))
        {
            renderedContent = "alert('Yaptığınız seçim sonucunda sonuçları görüntülenecek bayii bulunamamıştır.\\\r\\\n\\\r\\\nLütfen seçiminizi yenileyiniz.');";
            disabled = "disabled";
        }

        renderedContent += "var inputs = document.getElementsByTagName('input'); for(var i = 0; i < inputs.length; i++) { if(inputs[i].type == 'submit') inputs[i].disabled = '" + disabled + "'; }";
        Response.Write(renderedContent);
    }

    private const string NON_VIRTUAL_DEALER_TYPE_CODES = "[dealer][branch][satellite][connected_point][network1][network2]";

    private string renderDealerList(bool renderVirtualsOnly)
    {
        dataCache projectDataCache = getDataCache();

        StringBuilder htmlBuilder = new StringBuilder();

        if (base.calculationType == calculatorType.network1 || base.calculationType == calculatorType.network2)
        {
            appendDealerAndChildren(projectDataCache.getDealer("TR"), htmlBuilder, renderVirtualsOnly, "", base.calculationType.ToString());
        }
        else if (base.calculationType == calculatorType.allnetworks)
        {
            appendDealerAndChildren(projectDataCache.getDealer("TR"), htmlBuilder, renderVirtualsOnly, "");
            appendDealerAndChildren(projectDataCache.getDealer("S-1"), htmlBuilder, true, "");
            appendDealerAndChildren(projectDataCache.getDealer("S-2"), htmlBuilder, true, "");
        }
        else if (base.calculationType != calculatorType.connectedPoints)
        {
            appendDealerAndChildren(projectDataCache.getDealer("TR"), htmlBuilder, renderVirtualsOnly, "");
            appendDealerAndChildren(projectDataCache.getDealer("SBG"), htmlBuilder, renderVirtualsOnly, "");
            appendDealer(projectDataCache.getDealer("FG"), htmlBuilder, "");
            if (!renderVirtualsOnly)
            {
                appendDealer(projectDataCache.getDealer(base.calculationType == calculatorType.dealersAndSatellites ? "S-3" : "S-1"), htmlBuilder, "");
            }
        }
        else
        {
            if (projectDataCache.getDealer("S-2") != null)
            {
                appendDealerAndChildren(projectDataCache.getDealer("S-2"), htmlBuilder, renderVirtualsOnly, "");
            }
        }
        return htmlBuilder.ToString();
    }

    private void appendDealer(dealerDefinition dealer, StringBuilder htmlBuilder, string hierarchy)
    {
        htmlBuilder.AppendFormat("addOption('d', '{0}', '{1}', {2});", dealer.getCode(), hierarchy + dealer.getCode() + " " + dealer.getName(), dealer.getCode() == base.getString("d") ? "1" : "0");
    }

    private void appendDealerAndChildren(dealerDefinition dealer, StringBuilder htmlBuilder, bool renderVirtualsOnly, string hierarchy)
    {
        appendDealerAndChildren(dealer, htmlBuilder, renderVirtualsOnly, hierarchy, null);
    }

    private void appendDealerAndChildren(dealerDefinition dealer, StringBuilder htmlBuilder, bool renderVirtualsOnly, string hierarchy, string typeFilter)
    {
        if (renderVirtualsOnly && NON_VIRTUAL_DEALER_TYPE_CODES.Contains("[" + dealer.getTypeCode() + "]")) return;

        string[] userDealerCodes = base.getUserDealerCodes();

        bool continueFlag = false;
        foreach (string userDealerCode in userDealerCodes)
        {
            if (dealer.getCode() == "TR" || dealer.isParentOfAny(userDealerCodes) || dealer.getCode() == userDealerCode)
            {
                continueFlag = true;
                break;
            }
        }

        if (!continueFlag)
        {
            return;
        }

        if
        (
            dealer.getCode() == "TR"
            ||
            (renderVirtualsOnly && NON_VIRTUAL_DEALER_TYPE_CODES.Contains("[" + dealer.getTypeCode() + "]"))
            ||
            (!renderVirtualsOnly && !NON_VIRTUAL_DEALER_TYPE_CODES.Contains("[" + dealer.getTypeCode() + "]"))
            ||
            (string.IsNullOrEmpty(typeFilter) || dealer.getTypeCode() == typeFilter)
        )
        {
            appendDealer(dealer, htmlBuilder, hierarchy);
        }

        hierarchy += "&nbsp;&nbsp;&nbsp;";

        if (!(base.calculationType == calculatorType.dealersAndSatellitesJoined && (dealer.getTypeCode() == "branch" || dealer.getTypeCode() == "dealer")))
        {
            List<dealerDefinition> dealerListToRender = new List<dealerDefinition>(dealer.children);

            dealerListToRender.Sort(new dealerListSorter(dealerListSortType.sortByCode));

            foreach (dealerDefinition child in dealerListToRender)
            {
                appendDealerAndChildren(child, htmlBuilder, renderVirtualsOnly, hierarchy, typeFilter);
            }
        }
    }
}
